package org.dromara.mall.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.mall.domain.MallOrders;
import org.dromara.mall.service.IMallOrdersService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 全部订单
 *
 * @author Mr Lei
 * @date 2024-05-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/orders")
public class MallOrdersController extends BaseController {

    private final IMallOrdersService mallOrdersService;

    /**
     * 查询全部订单列表
     */
    @SaCheckPermission("mall:orders:list")
    @GetMapping("/list")
    public TableDataInfo<MallOrders> list(MallOrders bo, PageQuery pageQuery) {
        return mallOrdersService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出全部订单列表
     */
    @SaCheckPermission("mall:orders:export")
    @Log(title = "全部订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MallOrders bo, HttpServletResponse response) {
        List<MallOrders> list = mallOrdersService.queryList(bo);
        ExcelUtil.exportExcel(list, "全部订单", MallOrders.class, response);
    }

    /**
     * 获取全部订单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mall:orders:query")
    @GetMapping("/{id}")
    public R<MallOrders> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(mallOrdersService.queryById(id));
    }

    /**
     * 新增全部订单
     */
    @SaCheckPermission("mall:orders:add")
    @Log(title = "全部订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MallOrders bo) {
        return toAjax(mallOrdersService.insertByBo(bo));
    }

    /**
     * 修改全部订单
     */
    @SaCheckPermission("mall:orders:edit")
    @Log(title = "全部订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MallOrders bo) {
        return toAjax(mallOrdersService.updateByBo(bo));
    }

    /**
     * 删除全部订单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mall:orders:remove")
    @Log(title = "全部订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(mallOrdersService.deleteWithValidByIds(List.of(ids), true));
    }
}
